package nero.yncc.com.createart.activity;

import android.text.TextUtils;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterFoundArticle;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanMessage;
import nero.yncc.com.createart.utils.HttpUtils;
import nero.yncc.com.createart.utils.UserDataUtil;

/**
 * Created by nero on 2017/7/12.
 */

@ContentView(R.layout.single_pulltorefresh)
public class ActivityArticleList extends BaseActivity {
    @ViewInject(R.id.pl_list)
    PullToRefreshListView pl_list;


    String title, id;
    ArrayList<BeanMessage> arrayList_article;
    AdapterFoundArticle adapterFoundArticle;

    int page=1,pageSize=10;
    boolean canLoad=true;
    @Override
    protected void initData() {
        super.initData();
        title = getIntent().getStringExtra("title");
        id = getIntent().getStringExtra("id");
        arrayList_article=new ArrayList<>();
        adapterFoundArticle=new AdapterFoundArticle(mContext,arrayList_article);
        pl_list.setMode(PullToRefreshBase.Mode.BOTH);
        pl_list.setAdapter(adapterFoundArticle);
        setTitleBarTitle(title);
    }

    @Override
    protected void initListener() {
        super.initListener();
        pl_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                canLoad = true;
                arrayList_article.clear();
                adapterFoundArticle.notifyDataSetChanged();

                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getData();
            }
        });
        pl_list.setRefreshing(true);
    }

    @Override
    protected void getData() {
        super.getData();
        if (Constants.hashmap_type.size() == 0) {
            UserDataUtil.getArticleType(this);
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            getData();
                        }
                    });

                }
            }, 1000);
            return;
        }

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("pagesize", pageSize + "");
        hashMap.put("pageindex", page + "");
        hashMap.put("category_id", id);

//        "count":149,
//                "list":[
        HttpUtils.get(this, Constants.API_ARTICLE_LIST, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                JSONArray jsonArray = null;
                try {
                    jsonArray = new JSONObject(json.toString()).getJSONArray("list");
                    if (!canLoad) {
                        showToast("没有更多内容了");
                        pl_list.onRefreshComplete();
                        return;
                    }
                    if (jsonArray.length() < pageSize) {
                        canLoad = false;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("获取列表失败，请再次尝试");
                    return;
                }


                for (int a = 0; a < jsonArray.length(); a++) {
                    try {
                        arrayList_article.add(JSON.parseObject(jsonArray.getString(a), BeanMessage.class));

                    } catch (Exception e) {

                    }
                }

                adapterFoundArticle.notifyDataSetChanged();
                pl_list.onRefreshComplete();
            }

            @Override
            public void fail(String failMsg) {
                showToast(failMsg);
                pl_list.onRefreshComplete();
            }
        });

    }
}
